/**
	Copyright (c) 2010 ATTi 
	All rights reserved.
	Author: Michael Shu
	Date: 5/25/2010
*/

@isTest
private class EmailSentFromCaseHelperClassTester {
	public static Integer NUM = 2;
	public static testMethod void testSendEmailtoCaseOwner() {
		List<User> users  = new List<User>();
		for(Integer i = 0; i < NUM; i++){
			User u_new = new User();
			u_new.UserName = 'xyz_1234567890@abcd.com' + i;
			u_new.LastName = 'Michael Shu Test Email' + i;
			u_new.Email = 'sshu@attinteractive.com';
			u_new.Alias = 'abcde' + i;
			u_new.CommunityNickname = 'xyaz_1234556' + i;
			u_new.TimeZoneSidKey = 'America/Los_Angeles';
			u_new.LocaleSidKey = 'en_US';
			u_new.EmailEncodingKey = 'UTF-8';
			u_new.ProfileId = [select id from Profile where Name like '%IOG Support - SuperUser - East%' limit 1].Id;
			u_new.UserRoleId = [select id from UserRole where Name like 'IOG EAST%' limit 1].Id ;
			u_new.LanguageLocaleKey = 'en_US';
			u_new.IsActive = true;
			insert u_new;
		}
		users = [select FirstName from User where UserName like 'xyz_1234567890@abcd.com%'];
		System.assertEquals(NUM, users.size());
		
	    User u = new User();
		u.UserName = 'xyz_1234567890@atti-temp.com' + 'x';
		u.LastName = 'Michael Shu Test Email' + 'x';
		u.Email = 'xyz_1234567890@atti.com';
		u.Alias = 'abcde' + 'x';
		u.CommunityNickname = 'xyaz_1234556' + 'x';
		u.TimeZoneSidKey = 'America/Los_Angeles';
		u.LocaleSidKey = 'en_US';
		u.EmailEncodingKey = 'UTF-8';
		u.ProfileId = [select ID from Profile where Name = 'System Administrator' limit 1].Id;
		u.UserRoleId = [select ID from UserRole where Name = 'Information Systems' limit 1].Id;
		u.LanguageLocaleKey = 'en_US';
		Database.Saveresult sr = Database.insert(u);
		
		User sysUser = [select ID from User where ID =: sr.getID() limit 1];
		
		String csSubject = 'testSubject_email';
		String csDescription = 'This is for email test only';
		Case newCase = new Case(subject = csSubject
		                      , description = csDescription
		                      , IR_Priority__c = 'Standard'
		                      , recordTypeId = RecordTypeHelper.GetRecordTypeID('Case',  'IOG Incident'));
		System.runAs(users[0]){
			insert newCase;
		}
		Case curCase = [select Id from Case where OwnerId =: users[0].Id limit 1];
		Set<ID> caseIds = new Set<Id>{curCase.Id};
		String Case_Comment = 'Case Comment';
		Boolean rst;
		System.runAs(sysUser){
			rst = EmailSentFromCaseHelperClass.sendEmailtoCaseOwnerIfDifferent(caseIds, Case_Comment);
		}
		System.assertEquals(true, rst);	
		
		curCase.IR_Priority__c = 'Escalation';
		System.runAs(sysUser){
			update curCase;	
		}
		System.assertEquals(true, rst);
		
		CaseComment cComment = new CaseComment();
        cComment.CommentBody = 'TestTriggerOnly';
        cComment.ParentId = curCase.Id;
        System.runAs(sysUser){
        	insert cComment;
        }
        CaseComment c_rst = [select IsPublished from CaseComment where CommentBody =: 'TestTriggerOnly'];
        System.assertEquals(true, rst);
	}
}